package my.codeandroid.antollroid;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseAdapter {
	private final String TABLE_FARES = "fares";
	private final String TABLE_PLAZAS = "plazas";
	public static final String PLAZA_CODE = "code";
	public static final String PLAZA_NAME = "name";
	public static final String CLASS_1 = "class_1";
	public static final String CLASS_2 = "class_2";
	public static final String CLASS_3 = "class_3";
	public static final String CLASS_4 = "class_4";
	public static final String CLASS_5 = "class_5";
	public static final String FESTIVE_RATE = "festive_rate";
	public static final String OFFPEAK_RATE = "offpeak_rate";
	public static final String DISTANCE = "distance";
	
	private SQLiteDatabase database;
	
	/*
	 * Connect to the antollroid_db. On a fresh installation to this application,
	 * the DatabaseHelper will copy db (from asset folder) to persistence storage.
	 */
	public DatabaseAdapter(Context context) {
		database = new DatabaseHelper(context).getReadableDatabase();
	}
	
	/*
	 * MUST be called before application end.
	 */
	public void close() {
		database.close();
	}
	
	public Cursor fetchAllPlazas() {
		return database.query(TABLE_PLAZAS, null, null, null, null, null, PLAZA_NAME);
	}
	
	public Cursor getFares(String startCode, String endCode) {
		return database.query(TABLE_FARES, 
				new String[] {CLASS_1, CLASS_2, CLASS_3, CLASS_4, CLASS_5, FESTIVE_RATE, OFFPEAK_RATE, DISTANCE}, 
				"start='"+startCode+"' AND end='"+endCode+"'", 
				null, // XXX Bug: selectionArgs is not working 
				null, 
				null, 
				null,
				"1");
	}
}